package com.huluobo.a2112mvvmdouyindemo.home;


import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;


import androidx.annotation.NonNull;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.GridLayoutManager;

import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.huluobo.a2112mvvmdouyindemo.R;
import com.huluobo.a2112mvvmdouyindemo.adapter.HomeTabAdapter;
import com.huluobo.a2112mvvmdouyindemo.databinding.FragmentHomeTabBinding;
import com.huluobo.mvvm_base.bean.HomeTabBannerBeanResult;
import com.huluobo.mvvm_base.bean.HomeVideoBeanResult;
import com.huluobo.mvvm_base.view.BaseMVVMFragment;
import com.youth.banner.BannerConfig;
import com.youth.banner.loader.ImageLoader;

import java.util.ArrayList;
import java.util.List;

public class HomeTabFragment extends BaseMVVMFragment<FragmentHomeTabBinding, HomeViewModel> {
    //首页tab的适配器
    private HomeTabAdapter homeTabAdapter;
    //首页tab的数据源
    private List<HomeVideoBeanResult.HomeVideoBean> homeVideoBeans = new ArrayList<>();

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_home_tab;
    }

    @Override
    protected void initData() {
        viewModel.getHomeVideoSuccess.observe(this, homeVideoBeanResult -> {
            if (homeVideoBeanResult.getCode() == 200) {
                homeVideoBeans.clear();
                homeVideoBeans.addAll(homeVideoBeanResult.getData());
                homeTabAdapter.notifyDataSetChanged();

                homeTabAdapter.setOnItemClickListener((adapter, view, position) -> {
                    Intent intent = new Intent(requireContext(), PlayActivity.class);
                    intent.putExtra("path", homeVideoBeans.get(position).getVideopath());
                    intent.putExtra("title", homeVideoBeans.get(position).getCaption());
                    startActivity(intent);
                });
            } else {
                ToastUtils.showLong(homeVideoBeanResult.getMessage());
            }
        });

        viewModel.getHomeVideoFailed.observe(this, s -> {
            Log.i("TagA", "homeVideoFailed:" + s);
        });

        viewModel.getHomeBannerSuccess.observe(this, homeTabBannerBeanResult -> {
            //由于banner的设置图片只接收String(图片地址)类型的数据,所以需要从beanResult中取出集合,再循环赋值一次
            List<String> banners = new ArrayList<>();
            for (int i = 0; i < homeTabBannerBeanResult.getData().size(); i++) {
                banners.add(homeTabBannerBeanResult.getData().get(i).getImagePath());
            }
            //以下为额外内容,可以不再banner中添加
            List<String> bannerTitles = new ArrayList<>();
            for (int i = 0; i < homeTabBannerBeanResult.getData().size(); i++) {
                bannerTitles.add(homeTabBannerBeanResult.getData().get(i).getDesc());
            }
            dataBinding.homeTabBanner.setBannerTitles(bannerTitles);
            //额外内容结束
            dataBinding.homeTabBanner.setImages(banners);
            dataBinding.homeTabBanner.start();
        });

        viewModel.getHomeBannerFailed.observe(this, s -> {
            Log.i("TagA", "homeBannerFailed:" + s);
        });
    }

    @Override
    protected void initView() {
        //初始化请求数据列表
        viewModel.getHomeVideoList(1, 10);
        //初始化时请求banner
        viewModel.getHomeBanner();

        //初始化视频列表
        dataBinding.homeTabRv.setLayoutManager(new GridLayoutManager(requireContext(), 2));
        homeTabAdapter = new HomeTabAdapter(homeVideoBeans);
        dataBinding.homeTabRv.setAdapter(homeTabAdapter);


        //banner初始化
        dataBinding.homeTabBanner.setBannerStyle(BannerConfig.NUM_INDICATOR_TITLE);//设置banner的圆点指示器
        //设置banner图片加载器
        dataBinding.homeTabBanner.setImageLoader(new ImageLoader() {
            @Override
            public void displayImage(Context context, Object path, ImageView imageView) {
                com.huluobo.a2112mvvmdouyindemo.utils.ImageLoader
                        .getInstance().loadImage(requireActivity(), path.toString(), imageView);
            }
        });
        //设置banner的切换延时时间
        dataBinding.homeTabBanner.setDelayTime(1500);
        //设置指示器的位置
        dataBinding.homeTabBanner.setIndicatorGravity(BannerConfig.CENTER);
    }

    @Override
    protected Class<HomeViewModel> getViewModel() {
        return HomeViewModel.class;
    }
}